package cn.edu.tsinghua.smxq.mobilecity.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddQuoteServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -62671538434998304L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		PrintWriter output;
		ResultSet result;
		response.setContentType("text/html; charset=gb2312");
		output = response.getWriter();
		String userId = request.getParameter("userId");
		String cityId = request.getParameter("cityId");
		String foodId = request.getParameter("foodId");
		String price = request.getParameter("price");
		String priceDate = "";
		try {
			priceDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(request.getParameter("priceDate"));
		} catch (Exception e) {
			priceDate = request.getParameter("priceDate");
		}
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());
		try {
			Connection con = DataBaseDriver.getConnection();
			Statement sql = con.createStatement();
			sql.execute("INSERT INTO foodquote(userid,cityid,foodid,price,pricedate,time)"
					+ " values("
					+ userId
					+ ","
					+ cityId
					+ ","
					+ foodId
					+ ","
					+ price + ",'" + priceDate + "','" + time + "')");
			output.write("success");
			sql.close();
			con.close();

		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("ClassNotFoundException:" + e.getMessage());

			output.write("failure");
		} catch (SQLException ex) {
			System.err.println("SQLException:" + ex.getMessage());
			output.write("failure");
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		PrintWriter output;
		ResultSet result;
		response.setContentType("text/html; charset=gb2312");
		output = response.getWriter();
		String userId = request.getParameter("userId");
		String cityId = request.getParameter("cityId");
		String foodId = request.getParameter("foodId");
		String price = request.getParameter("price");
		String priceDate = "";
		try {
			priceDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(request.getParameter("priceDate"));
		} catch (Exception e) {
			priceDate = request.getParameter("priceDate");
		}
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());
		try {
			Connection con = DataBaseDriver.getConnection();
			Statement sql = con.createStatement();
			sql.execute("INSERT INTO foodquote(userid,cityid,foodid,price,pricedate,time)"
					+ " values("
					+ userId
					+ ","
					+ cityId
					+ ","
					+ foodId
					+ ","
					+ price + ",'" + priceDate + "','" + time + "')");
			output.write("success");
			sql.close();
			con.close();

		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("ClassNotFoundException:" + e.getMessage());

			output.write("failure");
		} catch (SQLException ex) {
			System.err.println("SQLException:" + ex.getMessage());
			output.write("failure");
		}

	}

}
